<template>
  <div class="changePwd">
    <!-- 头部 -->
    <van-nav-bar
      style="background-color:#f2f2f2"
      title="修改密码"
      left-text=""
      right-text=""
      left-arrow
      @click-left="onClickLeft"
    />

    <van-form @submit="onSubmit">
      <van-field
        v-model="initPwd"
        type="password"
        name="initPwd"
        label="原始密码"
        placeholder="请输入原始密码"
      />
      <van-field
        v-model="newPwd"
        type="password"
        name="newPwd"
        label="新的密码"
        placeholder="请输入新的密码"
      />
      <van-field
        v-model="confirmNewPwd"
        type="password"
        name="confirmNewPwd"
        label="确认密码"
        placeholder="请确认新的密码"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" :loading="loading">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import SERVE from "@/axios/index";
export default {
  name: "changePwd",
  data() {
    return {
      loading:false,
      initPwd: "",
      newPwd: "",
      confirmNewPwd: "",
    };
  },
  mounted() {},
  methods: {
    onClickLeft() {
      this.$router.push("/vip");
    },
    onSubmit(values) {
      if (this.initPwd === '') {
        return this.$notify({ type: "warning", message: "请输入原始密码" });
      }
      if ( this.newPwd === "" || this.confirmNewPwd === "" || this.newPwd !== this.confirmNewPwd ) {
        return this.$notify({
          type: "warning",
          message: "请输入新的密码并确认两次输入无误",
        });
      }
      this.loading = true;
      SERVE.uploadPwd({
        params: {
          uid: window.localStorage.u_id,
          mima0: values.initPwd,
          mima1: values.newPwd,
        },
      }).then((res) => {
          this.loading = false;
          this.$notify({
            type: res.data.zt === "yes" ? "success" : "warning",
            message: res.data.xinxi,
            duration: 1000,
          });
          let that = this;
          if (res.data.zt === "yes") {
            window.localStorage.removeItem("u_login");
            window.localStorage.removeItem("u_id");
            setTimeout(() => {
              that.$router.push("/index");
            }, 1000);
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log("err", err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.changePwd {
  height: 100vh;
  background-color: #f2f2f2;
}
</style>
